package model;

import entity.Account;
import entity.ClassRoomBlock;
import entity.Mark;
import entity.Student;
import entity.Subject;
import entity.TeacherClassDetails;
import iface.IModel;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class MarkTableModel extends AbstractTableModel implements IModel<Mark> {

    private manager.Manager manager;
    private ArrayList<Mark> alMark;
    private Account account;

    public MarkTableModel(manager.Manager manager, Account account) {
        this.manager = manager;
        this.account = account;
    }

    @Override
    public int getRowCount() {
        int count = 0;
        if (account.getAuthorization() == 1) {
            for (int i = 0; i < this.manager.getMarkManager().size(); i++) {
                count++;
            }
        } else {
            ArrayList<TeacherClassDetails> alDetails = this.manager.getTeacherClassDetailsManager().getByTeacherID(Integer.valueOf(account.getAccountID()));
            ArrayList<Student> alStudent = new ArrayList<Student>();
            alMark = new ArrayList<Mark>();
            for (TeacherClassDetails details : alDetails) {
                ArrayList<Student> al = this.manager.getStudentManager().getByClassID(details.getClassID());
                alStudent.addAll(al);
            }
            for (Student student : alStudent) {
                ArrayList<Mark> al = this.manager.getMarkManager().getByStudentId(student.getStudentID());
                alMark.addAll(al);
            }
            count = alMark.size();
        }
        return count;
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Student student = null;
        Subject subject = null;
        ClassRoomBlock block = null;
        Mark obj = null;

        if (this.account.getAuthorization() == 1) {
            obj = this.manager.getMarkManager().get(rowIndex);
        } else {
            obj = this.alMark.get(rowIndex);
        }

        student = this.manager.getStudentManager().getById(obj.getStudentID());
        subject = this.manager.getSubjectManager().getById(obj.getSubjectID());
        block = this.manager.getClassRoomBlockManager().getById(subject.getClassRoomBlockID());
        switch (columnIndex) {
            case 0:
                return block.getClassRoomBlockName();
            case 1:
                return subject;
            case 2:
                return student;
            case 3:
                return obj.getFirstSemesterValue();
            case 4:
                return obj.getSecondSemesterValue();
            case 5:
                return obj.getYearValue();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Khối";
            case 1:
                return "Tên Môn Học";
            case 2:
                return "Tên Học Sinh";
            case 3:
                return "Điểm Học Kỳ I";
            case 4:
                return "Điểm Học Kỳ II";
            case 5:
                return "Điểm Cuối Năm";
            default:
                return null;
        }
    }

    @Override
    public void add(Mark obj) {
        this.manager.getMarkManager().add(obj);
        this.fireTableDataChanged();
    }

    @Override
    public void replace(int index, Mark obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void remove(Mark obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void refresh() {
        this.fireTableDataChanged();
    }

    @Override
    public Mark get(int index) {
        if (this.account.getAuthorization() == 1) {
            return this.manager.getMarkManager().get(index);
        } else {
            return this.alMark.get(index);
        }
    }
}